import ThreeRender from "./ThreeRender";
import ThreeScene from './ThreeScene.js';
import ThreeCamera from './ThreeCamera';
import ThreeGrid from "./ThreeGrid";
import FactoryGeometry from "./factory/FactoryGeometry";
import FactoryMaterial from "./factory/FactoryMaterial";
import UtilRandom from "@/tool/ToolRandom";
import ThreeRaycaster from "./ThreeRaycaster";
import ThreeRain from './ThreeRain';
import TestLoader from "./loader/TestLoader";
import ThreeSpriteText from "./ThreeSpriteText";
import FactoryText from "./factory/FactoryText";
import * as THREE from "three";
import Stats from "@/components/flush-stats";

let _dom
let scene;
let camera;

function init({
                  dom,
                  showAxesHelperTag = false,
                  showGridTag = false,
                  ambientLight = 0x444444,
                  obj3ds = [],
                  loadModalPath,
                  point,
                  cameraFar,
                  cameraPosition
              }) {
    if (!dom) {
        console.error('ntd-three 模块初始化失败 - dom元素不能为空！')
        return
    }

    _dom = dom

    Stats.flush(dom)

    // 场景初始化
    scene = ThreeScene.initDefault()

    // 相机初始化
    camera = ThreeCamera.initCamera(75, _dom.clientWidth / _dom.clientHeight, 0.1, cameraFar, cameraPosition);
    scene.add(camera)

    if (showAxesHelperTag) {
        // 坐标系初始化 - 参数设置了三条轴线的长度
        scene.add(new THREE.AxesHelper(2000));
    }
    if (showGridTag) {
        // 地面网格初始化
        scene.add(ThreeGrid.init());
    }

    // 初始化环境光
    scene.add(new THREE.AmbientLight(ambientLight))

    // 初始化点光源
    scene.add(point || initPoint())

    // 渲染器初始化
    dom.appendChild(ThreeRender.initRender(_dom.clientWidth, _dom.clientHeight, scene, camera))

    // 初始化选择器
    ThreeRaycaster.init(scene, camera, dom)

    // 添加初始化场景内容
    obj3ds.forEach(scene.add)

    if (loadModalPath) {
        // 添加3D模型
        setTimeout(() => {
            TestLoader.loaderModel(function (obj) {
                scene.add(obj)
            }, loadModalPath)
        }, 1000)
    }
}

function add(content) {
    scene.add(content)
}

/**
 * 光源设置
 */
function initPoint() {
    // 点光源
    let point = new THREE.PointLight(0xffffff);
    point.position.set(0, 5000, 0); //点光源位置
    return point;
}

/**
 * 创建3d线条
 */
function createLine() {
    // 划线的材质
    let lineMaterial = new THREE.LineBasicMaterial({color: 0x0000ff});
    // 创建线坐标
    let lineGeometry = new THREE.BufferGeometry();

    lineGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array([
        -20, 0, 0,
        0, 20, 0,
        20, 0, 0
    ]), 3))

    let line = new THREE.Line(lineGeometry, lineMaterial);

    setInterval(() => {
        line.rotateY(0.03)
    }, 30)

    return line;
}

let _index = 0;

function randomElement(geometry = FactoryGeometry.getByIndex(UtilRandom.randomNum(0, 5))) {
    // 网格
    let cube = new THREE.Mesh(geometry, FactoryMaterial.getByIndex(UtilRandom.randomNum(0, 2)));

    cube.name = _index++ + '号对象'

    cube.position.set(UtilRandom.randomNum(-500, 500), UtilRandom.randomNum(0, 500), UtilRandom.randomNum(-500, 500));

    return ThreeScene.add(cube);
}

function openRain() {
    ThreeRain.init(scene)
}

function closeRain() {
    ThreeRain.close()
}

function addText(text) {
    let textObj = ThreeSpriteText.makeTextSprite(text,
        {
            fontsize: 20,
            borderColor: {r: 255, g: 0, b: 0, a: 0.4},/* 边框黑色 */
            backgroundColor: {r: 255, g: 255, b: 255, a: 0.9}/* 背景颜色 */
        });
    textObj.position.set(200, 200, 200)
    ThreeScene.add(textObj)
}

function add3DText(text) {
    FactoryText.createText(text).then(geometry => {
        randomElement(geometry);
    })
}

export default {
    init,
    randomElement,
    openRain,
    closeRain,
    addText,
    add3DText,
    add
}
